package com.example.shop_2207.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2207.entity.GoodsEntity
import com.example.shop_2207.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchViewModel:BaseViewModel() {
    val searchSuccess = MutableLiveData<List<GoodsEntity>>()//和之前不同了，这只需list---data   因为没有接口，是从其他接口中获取部分数据进行展示
    val searchFail = MutableLiveData<String>()

    fun search(key:String){
        viewModelScope.launch {
            try {
                val result = apiServer.goods(0,1,100)
                val data = result.data.filter {
                    key in it.goods_desc
                    //it.goods_desc.contains(key) //两种方法均能实现
                }//过滤 关键词 在描述中   属于符合条件的内容
                searchSuccess.value = data
            }catch (e:Exception){
                searchFail.value = e.message
            }
        }
    }
}